<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class ManagerController extends CommonController{
	//管理员列表
	public function index(){
		$pageNum=I('pageNum',1,intval);//第几页
		$numPerPage=I('numPerPage',10,intval);//每页显示条数
		//获取排序的字段及排序方式
		//
		//
		$orderField=$_POST['_order']?$_POST['_order']:'id';
		$orderDirection=$_POST['_sort']?$_POST['_sort']:'asc';

		//组合排序字符串
		$orders=($orderField.' '.$orderDirection);


		//$db=M('manager');
		$db=D('ManagerRelation');
		//获取总数据条数
		$count=$db->count();
		$this->assign('totalCount',$count);//数据的总条数
		$this->assign('numPerPage',$numPerPage);//每页显示多少条
		$this->assign('pageNumShown',6);//页标数字多少个
		$this->assign('currentPage',$pageNum);//当前是第几页
		$page= new \Think\Page($count,$numPerPage);
		$this->user=$db->limit($numPerPage)->Page($pageNum)->order($orders)->relation(true)->select();

		//权限菜单的显示与隐藏	
		$this->assign('manager_addId',5);//添加
		$this->assign('manager_delId',35);//删除
		$this->assign('manager_repwdId',109);//密码重置
		$this->assign('manager_groupId',110);//密码重置
		$this->assign('manager_depotId',111);//密码重置
		$this->assign('manager_statusId',112);//密码重置
		$this->assign('manager_editId',14);//编辑
		$this->display();

	}



	//修改密码界面
	public function repwd(){
		if(!empty($_POST)){
			//修改密码数据
			$img=new \Think\Verify();
			$result=$img->check(I('code'));
			if(!$result) $this->error('验证码错误');
			$User	 =	 D("manager");
			if(!$User->create()) {
				$this->error($User->getError());
			}else{
				// 修改帐号数据
				if($result=$User->save()) {
					$this->success('修改成功！');
				}else{
					$this->error('修改失败！');
				}
			}

		}else{
			$id = $_GET['id'];
			if(isset($id)){
				$user=M('manager')->find($id);
				if ($user) {
	                    $this->user=$user;
	                    $this->display();
	                } else {
	                    $this->error ('数据加载失败');
	            }
			}
		}
	}

		
	//更改用户组别
	Public function group(){
		if(!empty($_POST)){
			//处理更改用户的分组
			$uid=$_POST['uid'];
			$data=array();
			foreach ($_POST['group_id'] as $v) {
				$data[]=array(
					'uid'=>$uid,
					'group_id'=>$v
					);
			}
			$db=M('access');
			$db->where(array('uid'=>$uid))->delete();
			if($db->addAll($data)){
				$this->success('修改成功！');
			}else{
				$this->error('修改失败！');
			}
		}else{
			$id=$_GET['id'];
			$db=D('ManagerRelation');
			$field=array('id','username','realname');
			$user=$db->where(array('id'=>$id))->field($field)->relation('authgroup')->find();
			$this->assign('user',$user);
			foreach ($user['authgroup'] as $v) {
				$groups.=$v['id'].',';
			}
			$this->groups=rtrim($groups,',');
			$db1=M('authgroup');
			$this->group=$db1->field('id,title')->select();
			$this->display();
		}
	}

	//配置用户仓库权限
	Public function depot(){
		if(!empty($_POST)){
			//处理更改用户的分组
			$uid=$_POST['uid'];
			$data=array();
			foreach ($_POST['depot_id'] as $v) {
				$data[]=array(
					'uid'=>$uid,
					'depot_id'=>$v
					);
			}
			$db=M('userdepot');
			$db->where(array('uid'=>$uid))->delete();
			if($db->addAll($data)){
				$this->success('修改成功！');
			}else{
				$this->error('修改失败！');
			}
		}else{
			$id=$_GET['id'];
			$db=D('ManagerRelation');
			$field=array('id','username','realname');
			$user=$db->where(array('id'=>$id))->field($field)->relation('depot')->find();
			$this->assign('user',$user);
			foreach ($user['depot'] as $v) {
				$depots.=$v['id'].',';
			}
			$this->depots=rtrim($depots,',');
			$db1=M('depot');
			$this->depot=$db1->field('id,name')->select();
			$this->display();
		}

	}	

}

?>